from bluepy.btle import Scanner, DefaultDelegate
class ScanDelegate(DefaultDelegate): 
	def __init__(self): 
		DefaultDelegate.__init__(self)
	def handleDiscovery(self, dev, isNewDev, isNewData): 
		if isNewDev: 
			print "Discovered device", dev.addr 
		elif isNewData: 
			print "Received new data from", dev.addr



serdat = "0"
global olddata
name = "0"

def m_scan(a,b):
	olddata = "0"
	flag = 0
	scanner = Scanner().withDelegate(ScanDelegate()) 
	devices = scanner.scan(5.0)
	for dev in devices: 
		#print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi) 
		for (adtype, desc, value) in dev.getScanData(): 
			if desc == "16b Service Data":
				serdat = value	
				#print  serdat.find("ffff",0,4)
				flag = 1
			if desc == "Complete Local Name" and flag == 1:
				name = value	
				if (value).find("ZK:",0,3) == 0:
					#print "yes"
					return serdat			
				#if serdat.find("ffff",0,4):
				#	print "hello"		
	return 0
